package clientApp;

import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.AbstractListModel;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;

import communication.client.PlayerInteractionManager;
import communication.common.CommonData;
import communication.common.LoggedPlayers;

import clientApp.utils.RPG_Workstation_Container;

/**
 * 
 * @author Gabriel Pinheiro
 */
@SuppressWarnings("serial")
public class GameLobbyPlayer extends javax.swing.JPanel {

	LoggedPlayers loggedPlayers = null;
	RPG_Workstation_Container container;
	private PlayerInteractionManager player = null;

	public boolean ready = false;
	
	private static int timerStop = 2;
	private static int timerStart = timerStop + 5;
	private static Timer t;

	public GameLobbyPlayer(RPG_Workstation_Container rpg_Workstation_Container, PlayerInteractionManager player) {
		this.container = rpg_Workstation_Container;
		this.player = player;
		this.loggedPlayers = player.getLoggedPlayers();
		

		initComponents();
		refreshCharList();
	}

	public void refreshCharList() {
		jListCharacters.setModel(new AbstractListModel() {
			String[] strings = loggedPlayers.getPlayersReadyList().toArray(new String[loggedPlayers.getNumPlayers()]);

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		jListChatRoom.setModel(new AbstractListModel() {
			String self = player.getCharacter().getName();
			String[] strings = loggedPlayers.getPlayersChatList(self).toArray(new String[loggedPlayers.getNumPlayers()]);

			public int getSize() {
				return strings.length;
			}

			public Object getElementAt(int i) {
				return strings[i];
			}
		});
		jListChatRoom.setSelectedIndex(jListChatRoom.getModel().getSize() - 1);

		jScrollPane4.setViewportView(jListCharacters);
		jScrollPane1.setViewportView(jListChatRoom);

	}

	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jSeparator1 = new javax.swing.JSeparator();
		jSeparator2 = new javax.swing.JSeparator();
		jPanel1 = new javax.swing.JPanel();
		jScrollPane4 = new javax.swing.JScrollPane();
		jListCharacters = new javax.swing.JList();
		jLabel3 = new javax.swing.JLabel();
		jPanel2 = new javax.swing.JPanel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabelIp = new javax.swing.JLabel();
		jLabelMapName = new javax.swing.JLabel();
		jBReady = new javax.swing.JButton();
		jPanel3 = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jListChatRoom = new javax.swing.JList();
		jBSend = new javax.swing.JButton();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTAMSGS = new javax.swing.JTextArea();
		jTFText = new javax.swing.JTextField();

		setBackground(new java.awt.Color(0, 0, 102));
		setPreferredSize(new java.awt.Dimension(800, 600));

		jListChatRoom.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		jLabel1.setFont(new java.awt.Font("Tahoma", 0, 28));
		jLabel1.setForeground(new java.awt.Color(255, 255, 255));
		jLabel1.setText("Player's Game Lobby");

		jPanel1.setBackground(new java.awt.Color(0, 0, 102));
		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "   Choose Your Character   ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12), new java.awt.Color(255, 255, 255))); // NOI18N
		jPanel1.setMinimumSize(new java.awt.Dimension(780, 209));
		
		jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18));
		jLabel3.setForeground(new java.awt.Color(255, 255, 255));
		jLabel3.setText("Current Party:");

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE).addComponent(jLabel3)).addContainerGap()));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addGap(12, 12, 12).addComponent(jLabel3).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE).addContainerGap()));

		jPanel2.setBackground(new java.awt.Color(0, 0, 102));
		jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "   Start The Game   ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12), new java.awt.Color(255, 255, 255))); // NOI18N
		jPanel2.setMinimumSize(new java.awt.Dimension(780, 137));

		jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
		jLabel4.setForeground(new java.awt.Color(255, 255, 255));
		jLabel4.setText("Game Room IP:");

		jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
		jLabel5.setForeground(new java.awt.Color(255, 255, 255));
		jLabel5.setText("Starting Map:");

		jLabelIp.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabelIp.setForeground(new java.awt.Color(255, 255, 255));
		jLabelIp.setText(player.getHostIp());

		jLabelMapName.setFont(new java.awt.Font("Tahoma", 0, 12));
		jLabelMapName.setForeground(new java.awt.Color(255, 255, 255));
		jLabelMapName.setText(player.getCurrentMapFullPath());

		jBReady.setText("Ready");
		jBReady.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jBReadyActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE).addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)).addGap(18, 18, 18).addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabelIp).addComponent(jLabelMapName))).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup().addComponent(jBReady).addContainerGap()))));

		jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { jBReady });

		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel4).addComponent(jLabelIp)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel5).addComponent(jLabelMapName)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE).addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jBReady)).addContainerGap()));

		jPanel3.setBackground(new java.awt.Color(0, 0, 102));
		jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "   Chat   ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12), new java.awt.Color(255, 255, 255))); // NOI18N

		jBSend.setText("Send");
		jBSend.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jBSendActionPerformed();
			}
		});

		jTFText.addKeyListener(new KeyListener() {
			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
			}

			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER)
					if (jTFText.hasFocus())
						jBSendActionPerformed();
			}
		});

		jTAMSGS.setColumns(20);
		jTAMSGS.setEditable(false);
		jTAMSGS.setRows(5);
		jScrollPane2.setViewportView(jTAMSGS);

		javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jBSend, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(18, 18, 18).addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jTFText, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE).addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)).addContainerGap()));
		jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE).addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)).addGap(16, 16, 16).addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jBSend).addComponent(jTFText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap()));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE).addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup().addComponent(jLabel1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE))).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jLabel1).addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap()));
	}

	public void setHostInfo(String map, String ip) {
		jLabelMapName.setText(map);
		jLabelIp.setText(ip);
	}
	
	private void jBReadyActionPerformed(java.awt.event.ActionEvent evt) {
		if (ready) {
			ready = false;
			jBReady.setText("Ready");
			player.sendReady(player.getCharacter().getName(), ready);
		} else {
			ready = true;
			jBReady.setText("Not Ready");
		}
		player.sendReady(player.getCharacter().getName(), ready);
	}

	public void launchStatingSequence() {

		jBReady.setEnabled(false);

		player.showChatMessage("", "Our Master has summoned the Game Spirits and our journey is about to start in:");
		ActionListener action = new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				if (timerStart == timerStop) {
					player.showChatMessage("", "May the gods bless ours lives!");
					timerStart--;
				} else if (timerStart < 1) {
					t.stop();
					 container.add(player.getInGameScreen(), "InGameScreen");
					 container.panels.show(container, "InGameScreen");
				} else {
					if (timerStart - timerStop > 0)
						player.showChatMessage("", "" + (timerStart - timerStop));
					timerStart--;
				}

			}
		};
		t = new Timer(1000, action);
		t.start();
	}

	private void jBSendActionPerformed() {
		if (!jTFText.getText().isEmpty()) {
			String sender = player.getCharacter().getName();
			player.sendMessage(sender, jListChatRoom.getSelectedValue().toString(), jTFText.getText().trim());
			if (!jListChatRoom.getSelectedValue().toString().equals(CommonData.ALL_PLAYERS))
				sender += " to " + jListChatRoom.getSelectedValue().toString();
			showChatMessage(sender, jTFText.getText().trim());
			jTFText.setText("");
		}
	}

	/**
	 * Mostra a mensagem no chatbox
	 * 
	 * @param sender
	 *            O remetente
	 * @param message
	 *            A mensagem
	 */
	public void showChatMessage(String sender, String message) {
		if (!sender.isEmpty())
			sender += ": ";
		if (jTAMSGS.getText().isEmpty())
			jTAMSGS.setText(sender + message);
		else
			jTAMSGS.setText(jTAMSGS.getText() + "\n" + sender + message);
	}

	public javax.swing.JButton jBReady;
	private javax.swing.JButton jBSend;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabelIp;
	private javax.swing.JLabel jLabelMapName;
	private javax.swing.JList jListCharacters;
	private javax.swing.JList jListChatRoom;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JScrollPane jScrollPane4;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JSeparator jSeparator2;
	private javax.swing.JTextArea jTAMSGS;
	private javax.swing.JTextField jTFText;

}
